package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.constants.RedisConstant;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.common.sprinmvc.LeaseException;
import com.atguigu.lease.model.entity.SystemUser;
import com.atguigu.lease.model.enums.BaseStatus;
import com.atguigu.lease.web.admin.service.LoginService;
import com.atguigu.lease.web.admin.service.SystemUserService;
import com.atguigu.lease.web.admin.vo.login.CaptchaVo;

import com.atguigu.lease.web.admin.vo.login.LoginVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wf.captcha.SpecCaptcha;
import com.wf.captcha.base.Captcha;
import io.jsonwebtoken.JwtBuilder;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import org.springframework.util.ObjectUtils;

import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.TimeUnit;


@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    @Autowired
    private SystemUserService systemUserService;


    @Override
    public CaptchaVo sendCode() {

        //生成验证码  包含图片，图片中的文字
        SpecCaptcha specCaptcha = new SpecCaptcha(100, 48, 4);
        //生成字符串，数字等符号
        specCaptcha.setCharType(Captcha.TYPE_DEFAULT);
        //序列化验证码图片
        String base64 = specCaptcha.toBase64();

        //验证码字符串
        String text = specCaptcha.text();

        String uuidKey = RedisConstant.ADMIN_LOGIN_PREFIX + UUID.randomUUID().toString().replaceAll("-", "");

        redisTemplate.opsForValue().set(uuidKey, text, RedisConstant.ADMIN_LOGIN_CAPTCHA_TTL_SEC, TimeUnit.SECONDS);
        //拼接验证码和图片
        CaptchaVo captchaVo = new CaptchaVo(base64, uuidKey);

        return captchaVo;
    }

    @Override
    public String login(LoginVo loginVo) {

        //检查验证码是否过期  从Redis中获取
        String captchaKey = (String) redisTemplate.opsForValue().get(loginVo.getCaptchaKey());
        if (ObjectUtils.isEmpty(captchaKey)) {
            throw  new LeaseException(ResultCodeEnum.ADMIN_CAPTCHA_CODE_EXPIRED);
        }
        //检查验证码是否正确  将前端传过来的验证码和redis中验证码进行比较
        if (!captchaKey.equalsIgnoreCase(loginVo.getCaptchaKey())) {
            throw new LeaseException(ResultCodeEnum.ADMIN_CAPTCHA_CODE_ERROR);
        }
        //检查账号是否存在
        LambdaQueryWrapper<SystemUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SystemUser::getUsername, loginVo.getUsername());
        SystemUser systemUser = systemUserService.getOne(lambdaQueryWrapper);
        if (systemUser == null) {
            throw new LeaseException(ResultCodeEnum.ADMIN_ACCOUNT_NOT_EXIST_ERROR);
        }
        //检查输入的密码和数据库中的密码是否一致
        String md5Hex = DigestUtils.md5Hex(loginVo.getPassword());
        if (!systemUser.getPassword().equals(md5Hex)) {
            throw new LeaseException(ResultCodeEnum.ADMIN_ACCOUNT_ERROR);
        }
        //检查用户状态
        if (systemUser.getStatus() == BaseStatus.DISABLE) {
            throw new LeaseException(ResultCodeEnum.ADMIN_ACCOUNT_DISABLED_ERROR);
        }

        return "";
    }
}
